package com.viewpagertext.fragments;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.viewpagertext.R;
import com.viewpagertext.activitys.PlayActivity;
import com.viewpagertext.adapters.LocalGroupRecAdapter;
import com.viewpagertext.constructor.MessageEvent;
import com.viewpagertext.constructor.Song;
import com.viewpagertext.utils.LoadLocalMusicUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import static com.viewpagertext.adapters.MusicGridAdapter.FindImgUrlPath;
import static com.viewpagertext.adapters.MusicGridAdapter.FindSongsId;
import static com.viewpagertext.adapters.MusicGridAdapter.GroupFlag;

public class LocalGroupFragment extends Fragment implements LocalGroupRecAdapter.OnItemClickListener {

    private RecyclerView recyclerView;
    private List<Song> mLocalMusicList;
    private List<Song> filteredMusicList;
    private View view;
    private String flag;
    private LocalGroupRecAdapter localGroupRecAdapter;
    private EditText filterInfo;

    public LocalGroupFragment(String flag) {
        this.flag = flag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_local_group, container, false);
        mLocalMusicList = new ArrayList<>();

        newData();
        initView();
        return view;
    }

    // 更新数据
    private void newData() {
        mLocalMusicList.clear();
        mLocalMusicList.addAll(LoadLocalMusicUtils.getMusic());
        filteredMusicList = new ArrayList<>(mLocalMusicList);
    }

    private void initView() {
        recyclerView = view.findViewById(R.id.local_group_recyclerview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        localGroupRecAdapter = new LocalGroupRecAdapter(getActivity(), filteredMusicList, flag);
        localGroupRecAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(localGroupRecAdapter);
        filterInfo = view.findViewById(R.id.group_filter_info);
        // 搜索
        filterInfo.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                System.out.println("输入+++++");
                String searchText = editable.toString().toLowerCase();
                filteredMusicList.clear(); // 清空现有的过滤列表
                for (Song song : mLocalMusicList) {
                    if (song.song.concat(song.singer).concat(song.album).toLowerCase().contains(searchText)) {
                        filteredMusicList.add(song);
                    }
                }
                localGroupRecAdapter.notifyDataSetChanged(filteredMusicList);
            }
        });
    }

    @Override
    public void onItemClick(String groupName, List<Song> songs) {
        System.out.println("点击了分组: " + groupName);
        Intent intent = new Intent(getActivity(), PlayActivity.class);
        FindImgUrlPath = songs.get(0).picPath;//获取图片地址设置全局供ListFragment接收
//                Toast.makeText(mContext,"我的ID是："+FindSongsId,Toast.LENGTH_SHORT).show();
        //EventBus发送数据消息
        String ft = groupName;
        System.out.println("实现点击了" + ft);
        GroupFlag = true;
        EventBus.getDefault().postSticky(new MessageEvent(ft, songs));//发送
        getActivity().startActivity(intent);
    }
}